package com.huazi.www.frontdesk.util;

import java.sql.*;

/**
 * dao模式工具类
 */
public class BaseDao {
    private String driver = "com.mysql.cj.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/hospital?useUnicode=true&characterEncoding=utf8";
    //    private String url = "jdbc:mysql://localhost:3306/hospital";
    private String user = "root";
    private String pwd = "123456";


    public Connection conn = null;
    public PreparedStatement pstmt = null;

    /**
     * 链接数据库工具
     *
     * @return
     */
    public Connection conn() {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url, user, pwd);//链接数据库
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 关闭链接工具
     *
     * @param conn
     * @param statm
     * @param rs
     */
    public void closeShow(Connection conn, Statement statm, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (statm != null) {
            try {
                statm.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 增删改
     *
     * @param sql
     * @param patam
     * @return
     */
    public int executeUpdate(String sql, Object... patam) {
        PreparedStatement pstmt = null;
        conn = conn();
        int num = 0;
        try {
            pstmt = conn.prepareStatement(sql);
            if (pstmt != null) {
                for (int i = 0; i < patam.length; i++) {
                    pstmt.setObject(i + 1, patam[i]);
                }
            }
            num = pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeShow(conn, pstmt, null);
        }
        return num;
    }

    /**
     * 查询
     *
     * @param sql
     * @param patam
     * @return
     */
    public ResultSet executeQuery(String sql, Object... patam) {
        ResultSet rs = null;
        conn = conn();
        try {
            pstmt = conn.prepareStatement(sql);
            if (pstmt != null) {
                sql = "DROP  VIEW IF EXISTS performance;";
                for (int i = 0; i < patam.length; i++) {
                    pstmt.setObject(i + 1, patam[i]);
                }
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rs;
    }
}
